package cn.online.action;

import java.io.File;
import java.util.List;

import cn.online.model.Course;
import cn.online.model.Problem;
import cn.online.service.CourseService;
import cn.online.service.ProblemService;
import cn.online.util.MyFileUpload;

import com.opensymphony.xwork2.ActionSupport;

public class CourseAction extends ActionSupport {
	
	private CourseService courseService;
	private Course course;
	private List<Course> courseL;
	private MyFileUpload myFile = new MyFileUpload();
	private String imgFileName;
	private String imgContentType;
	private File img=null;
	private Integer cids[];
	private List<Problem> fproblemL;
	private List<Problem> nproblemL;
	private ProblemService problemService;
	
	
	public ProblemService getProblemService() {
		return problemService;
	}
	public void setProblemService(ProblemService problemService) {
		this.problemService = problemService;
	}
	public List<Problem> getFproblemL() {
		return fproblemL;
	}
	public void setFproblemL(List<Problem> fproblemL) {
		this.fproblemL = fproblemL;
	}
	public List<Problem> getNproblemL() {
		return nproblemL;
	}
	public void setNproblemL(List<Problem> nproblemL) {
		this.nproblemL = nproblemL;
	}
	public String getImgFileName() {
		return imgFileName;
	}
	public void setImgFileName(String imgFileName) {
		this.imgFileName = imgFileName;
	}
	public String getImgContentType() {
		return imgContentType;
	}
	public void setImgContentType(String imgContentType) {
		this.imgContentType = imgContentType;
	}
	public File getImg() {
		return img;
	}
	public void setImg(File img) {
		this.img = img;
	}
	public void setCids(Integer cids[]) {
		this.cids = cids;
	}
	public List getCourseL() {
		return courseL;
	}
	public Course getCourse() {
		return course;
	}
	public void setCourse(Course course) {
		this.course = course;
	}
	public void setCourseService(CourseService courseService) {
		this.courseService = courseService;
	}
	
	public String add(){
		
		if(img!=null){
			String type=imgContentType.toLowerCase();
			if("image/jgp".equals(type)||"image/jpeg".equals(type)){
					String pic = "/upload/"+myFile.fileUp(img, imgFileName);
					course.setCpic(pic);
			}else{
				return INPUT;
			}
		}
		courseService.insert(course);
		return SUCCESS;
	}
	
	public String load(){
		if(cids==null)
			return INPUT;
		int id=cids[0];
		course=courseService.findById(id);
		fproblemL=problemService.getListByFinish(id);
		nproblemL=problemService.getListByNew(id);
		return SUCCESS;
	}
	
	public String delete(){
		if(cids==null)
			return INPUT;
		for(int cid:cids)
			courseService.delete(cid);
		return SUCCESS;
	}
	
	public String update() throws Exception{
		if(img!=null){
			String type=imgContentType.toLowerCase();
			if("image/jgp".equals(type)||"image/jpeg".equals(type)){
					String pic = "/upload/"+myFile.fileUp(img, imgFileName);
					course.setCpic(pic);
			}else{
				return INPUT;
			}
		}
		courseService.update(course);
		return SUCCESS;
	}
	
	public String list(){
		courseL = courseService.findAll();
		return SUCCESS;
	}
}
